using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace System.Windows.Forms.Design
{
    internal class DegreeTypeEditor : System.Drawing.Design.UITypeEditor
    {
        private IWindowsFormsEditorService edSvc = null;

        public DegreeTypeEditor()
            : base()
        {
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null)
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc != null)
                {
                    DegreeEditor ed = new DegreeEditor();
                    ed.Size = new Size(78,78);
                    ed.ShowRadians = false;
                    ed.AngleChanged += new EventHandler(this.Popped);
                    ed.Angle = (float)value;
                    edSvc.DropDownControl(ed);
                    value = ed.Angle;
                    edSvc = null;
                }
            }

            return value;
        }

        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
        }

        private void Popped(object sender, EventArgs e)
        {
            if (edSvc != null)
                edSvc.CloseDropDown();
        }
    }

    internal class DegreeTypeEditorPopup : System.Windows.Forms.PopupForm
    {
        private System.ComponentModel.IContainer components = null;
        private DegreeEditor degreeEditor1;
        private System.Windows.Forms.Panel panel1;

        public DegreeTypeEditorPopup()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
            this.TopLevel = false;

            // TODO: Add any initialization after the InitializeComponent call
        }

        public float Value
        {
            get { return degreeEditor1.Angle; }
            set { degreeEditor1.Angle = value; }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void degreeEditor1_AngleChanged(object sender, System.EventArgs e)
        {
            this.Pop();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.degreeEditor1 = new DegreeEditor();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            //
            // panel1
            //
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                 this.degreeEditor1});
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(80, 80);
            this.panel1.TabIndex = 0;
            //
            // degreeEditor1
            //
            this.degreeEditor1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.degreeEditor1.Name = "degreeEditor1";
            this.degreeEditor1.Size = new System.Drawing.Size(78, 78);
            this.degreeEditor1.TabIndex = 0;
            this.degreeEditor1.Text = "0.0";
            this.degreeEditor1.AngleChanged += new System.EventHandler(this.degreeEditor1_AngleChanged);
            //
            // DegreeTypeEditorPopup
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(80, 80);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.panel1});
            this.Name = "DegreeTypeEditorPopup";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
        }
    }
}